/*
 * 
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
 * GNU General Public License for more details.
 * You should have received a copy of the GNU General Public License
 * along with this program. If not, see <http://www.gnu.org/licenses/>.
 */
package commandhandlers;

import input.IRCCommand;
import interfaces.CommandHandler;

import java.util.Calendar;

import main.CommonMethods;
import main.UserCentral;
import output.IRCOutput;
import botobjects.Discussion;
import botobjects.Post;

public class DiscussionHandler extends CommandHandler {
   private static DiscussionHandler INSTANCE = null;

   public static final DiscussionHandler getInstance() {
	  if (INSTANCE == null) {
		 INSTANCE = new DiscussionHandler();
	  }
	  return INSTANCE;
   }

   public static final DiscussionHandler newInstance() {
	  INSTANCE = null;
	  return getInstance();
   }

   private DiscussionHandler() {
   }

   @Override
   public void handle(final IRCCommand command, final IRCOutput output) {
   }

   public void handleCreatediscussion(final IRCCommand command, final IRCOutput out) {
	  if (command.size() >= 3 && "private".equalsIgnoreCase(command.get(1))
			&& UserCentral.getInstance().onlineUserIsAdmin(command.getSender(), command.getChannel())
			&& new Discussion(0, command.getPartialMessage(2, command.size()), true, false).saveToDB()) {
		 out.addOutput("Discussion added");
	  } else if (command.size() >= 2 && new Discussion(0, command.getPartialMessage(1, command.size()), false, false).saveToDB()) {
		 out.addOutput("Discussion added");
	  }
   }

   public void handleDiscussions(final IRCCommand command, final IRCOutput out) {
	  for (Discussion d : Discussion.getDiscussions(UserCentral.getInstance().onlineUserIsAdmin(command.getSender(),
			command.getChannel()))) {
		 out.addOutput(d.getIRCOutput(0));
	  }
   }

   public void handleEditpost(final IRCCommand command, final IRCOutput out) {
	  if (command.size() >= 3
			&& command.isInt(1)
			&& Post.editPost(Integer.parseInt(command.get(1)), UserCentral.getInstance().getOnlineUser(command.getSender()),
				  command.getPartialMessage(2, command.size()),
				  UserCentral.getInstance().onlineUserIsAdmin(command.getSender(), command.getChannel()))) {
		 out.addOutput("Post edited");
	  }
   }

   public void handleLockdiscussion(final IRCCommand command, final IRCOutput out) {
	  if (command.size() == 2 && command.isInt(1)) {
		 if (UserCentral.getInstance().onlineUserIsAdmin(command.getSender(), command.getChannel())
			   && Discussion.lockDiscussion(Integer.parseInt(command.get(1)), true)) {
			out.addOutput("Discussion locked");
		 }
	  }
   }

   public void handlePost(final IRCCommand command, final IRCOutput out) {
	  if (command.size() >= 3
			&& command.isInt(1)
			&& new Post(0, command.getPartialMessage(2, command.size()), UserCentral.getInstance().getOnlineUser(
				  command.getSender()), Calendar.getInstance().getTimeInMillis(), Integer.parseInt(command.get(1)), UserCentral
				  .getInstance().onlineUserIsAdmin(command.getSender(), command.getChannel())).saveToDB()) {
		 out.addOutput("Post added");
	  }
   }

   public void handleRead(final IRCCommand command, final IRCOutput out) {
	  if (command.size() == 2 && command.isInt(1)) {
		 for (Post post : Post.getPosts(Integer.parseInt(command.get(1)),
			   UserCentral.getInstance().onlineUserIsAdmin(command.getSender(), command.getChannel()))) {
			out.addOutput(post.getIRCOutput(0, CommonMethods.getUserTimeZone(command.getSender())));
		 }
	  }
   }

   public void handleRemovediscussion(final IRCCommand command, final IRCOutput out) {
	  if (command.size() == 2 && command.isInt(1)) {
		 if (UserCentral.getInstance().onlineUserIsAdmin(command.getSender(), command.getChannel())
			   && Discussion.deleteFromDB(command.get(1))) {
			out.addOutput("Discussion removed");
		 }
	  }
   }

   public void handleRemovepost(final IRCCommand command, final IRCOutput out) {
	  if (command.size() == 2
			&& command.isInt(1)
			&& Post.deleteFromDB(
				  String.valueOf(UserCentral.getInstance().onlineUserIsAdmin(command.getSender(), command.getChannel())),
				  command.get(1), UserCentral.getInstance().getOnlineUser(command.getSender()))) {
		 out.addOutput("Post removed");
	  }
   }

   public void handleUnlockdiscussion(final IRCCommand command, final IRCOutput out) {
	  if (command.size() == 2 && command.isInt(1)) {
		 if (UserCentral.getInstance().onlineUserIsAdmin(command.getSender(), command.getChannel())
			   && Discussion.lockDiscussion(Integer.parseInt(command.get(1)), false)) {
			out.addOutput("Discussion unlocked");
		 }
	  }
   }
}
